package com.agu.tasks;

import java.time.LocalDateTime;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * <p> 从数据库获取执行时间    任务不是异步的  一个一个执行
 * @author zrb
 *
 */

@Component
public class DynamicScheduleTask implements SchedulingConfigurer {

	@Mapper
	public interface CronMapper {
		@Select("select cron from cron limit 1")
		public String getCron();
	}

	@Autowired // 注入mapper
	CronMapper cronMapper;

	/**
	 * 执行定时任务.
	 */
	@Override
	public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

		taskRegistrar.addTriggerTask(
				// 1.添加任务内容(Runnable)
				() -> System.out.println("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),
				// 2.设置执行周期(Trigger)
				triggerContext -> {
					// 2.1 从数据库获取执行周期
					String cron = cronMapper.getCron();
					// 2.2 合法性校验.
					if (StringUtils.isEmpty(cron)) {
						// Omitted Code ..
					}
					// 2.3 返回执行周期(Date)
					return new CronTrigger(cron).nextExecutionTime(triggerContext);
				});
	}

}